package lib.quasar.base.frame;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import lib.quasar.base.R;
import lib.quasar.base.loading.LoadManage;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.util.GlideUtil;
import lib.quasar.util.LogUtil;
import lib.quasar.util.ToastUtil;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.chart.entry.Entry;
import lib.quasar.widget.sign.SignView;

public interface BaseView {

    default void beginLoading() {

        final LinearLayout layout = LoadManage.getManager().getLayout();
        final ViewGroup root = BaseApp.getRootView();
        root.removeView(layout);
        root.addView(layout);
        root.setEnabled(false);
    }

    default void closeLoading() {

        final LinearLayout layout = LoadManage.getManager().getLayout();
        final ViewGroup root = BaseApp.getRootView();
        root.removeView(layout);
        root.setEnabled(true);
    }

    default void goNext(Intent intent) {
        BaseApp.getActivity().startActivity(intent);
     //   BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void goNextResult(Intent intent, int requestCode) {
        BaseApp.getActivity().startActivityForResult(intent, requestCode);
      //  BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void goNext(Class<?> clazz) {

        final Context context = BaseApp.getContext();
        final Intent intent = new Intent(context, clazz);
        BaseApp.getActivity().startActivity(intent);
      //  BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void goNextResult(Class<?> clazz, int requestCode) {

        final Intent intent = new Intent(BaseApp.getContext(), clazz);
        BaseApp.getActivity().startActivityForResult(intent, requestCode);
       // BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void goNextKill(Class<?> clazz) {

        BaseApp.getActivity().onBackPressed();
        final Intent intent = new Intent( BaseApp.getContext(), clazz);
        BaseApp.getActivity().startActivity(intent);
     //   BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void goNextKill(Intent intent) {

        BaseApp.getActivity().onBackPressed();
       // BaseApp.getActivity().overridePendingTransition(0, 0);
        BaseApp.getActivity().startActivity(intent);
       // BaseApp.getActivity().overridePendingTransition(0, 0);
    }

    default void toast(String str) {

        if (TextUtils.isEmpty(str)) return;
        ToastUtil.showToast(str);
    }

    default void toast(int strId) {

        final Context context = BaseApp.getContext();
        final String strs = context.getResources().getString(strId);
        if (TextUtils.isEmpty(strs)) return;

        ToastUtil.showToast(strs);
    }

    default void setImage(ImageView image, Bitmap bitmap) {

        if (null == image || null == bitmap) return;

        image.setImageBitmap(bitmap);
        bitmap.recycle();
    }

    default void setImage(ImageView image, int resId) {

        if (null == image) return;
        image.setImageResource(resId);
    }

    default void setImage(ImageView image, CharSequence url) {

        if (null == image || TextUtils.isEmpty(url)) return;

        Context context = image.getContext();
        if (null == context) {
            context = BaseApp.getContext();
        }

        GlideUtil.loadImageSimple(context, image, url);
    }

    default void setImageRadius(ImageView image, CharSequence url, float radiussize) {

        if (null == image || TextUtils.isEmpty(url)) return;

        Context context = image.getContext();
        if (null == context) {
            context = BaseApp.getContext();
        }

        GlideUtil.loadImageRadius(context, image, url, radiussize);
    }

    default void setImage(ImageView image, CharSequence url, boolean circle) {

        if (null == image || TextUtils.isEmpty(url)) return;

        Context context = image.getContext();
        if (null == context) {
            context = BaseApp.getContext();
        }

        if (circle) {
            GlideUtil.loadImageCircle(context, image, url);
        } else {
            GlideUtil.loadImageSimple(context, image, url);
        }
    }

    default void setText(TextView text, CharSequence str) {

        if (null == text || null == str) return;
        text.setText(str);
    }

    default void setSelection(EditText text, int position) {

        if (null == text || position < 0) return;
        text.setSelection(position);
    }

    default void clearFocus(EditText text) {
        if (null == text) return;
        text.clearFocus();
    }

    default void setText(TextView text, int strId) {

        if (null == text) return;

        Context context = text.getContext();
        if (null == context) {
            context = BaseApp.getContext();
        }
        final String strs = context.getResources().getString(strId);
        text.setText(strs);
    }

    default void setTextColor(TextView text, int color) {

        if (null == text) return;

        try {
            Context context = text.getContext();
            if (null == context) {
                context = BaseApp.getContext();
            }
            final int color1 = context.getResources().getColor(color);
            text.setTextColor(color1);
        } catch (Exception e) {
            e.printStackTrace();
            text.setTextColor(color);
        }
    }

    default void setTextColor(TextView text, String color) {

        if (null == text || TextUtils.isEmpty(color)) return;

        try {
            text.setTextColor(Color.parseColor(color));
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        }
    }

    default void setImage(RecyclerHolder holder, int id, int resId) {

        if (null == holder) return;
        holder.setImageResource(id, resId);
    }

    default void setImage(RecyclerHolder holder, int id, String imageUrl) {

        if (null == holder || TextUtils.isEmpty(imageUrl)) return;
        ImageView image = holder.getView(id);
        if (null == image) return;
        setImage(image, imageUrl);
    }

    default void setImageRadius(RecyclerHolder holder, int id, String imageUrl, float radiussize) {

        if (null == holder || TextUtils.isEmpty(imageUrl)) return;
        ImageView image = holder.getView(id);
        if (null == image) return;
        setImageRadius(image, imageUrl, radiussize);
    }

    default void setText(RecyclerHolder holder, int id, CharSequence str) {

        if (null == holder) return;
        holder.setText(id, str);
    }

    default void setSign(RecyclerHolder holder, int id, int strId, int color) {

        if (null == holder) return;
        holder.setSign(holder, id, BaseApp.getResource().getString(strId), BaseApp.getResource().getColor(color));
    }

    default void setSign(RecyclerHolder holder, int id, String strId, int color) {

        if (null == holder) return;
        holder.setSign(holder, id, strId, BaseApp.getResource().getColor(color));
    }

    default void setSign(SignView sign, String str, int color) {

        if (null == sign || TextUtils.isEmpty(str)) return;
        sign.setSign(str, BaseApp.getResource().getColor(color));
    }

    default void setInputMax(RecyclerHolder holder, int id, float max) {

        if (null == holder) return;
        holder.setInputMax(id, max);
    }

    default void setInputMin(RecyclerHolder holder, int id, float max) {

        if (null == holder) return;
        holder.setInputMin(id, max);
    }

    default void setWarningMax(RecyclerHolder holder, int id, float max) {

        if (null == holder) return;
        holder.setMaxWarning(id, max);
    }

    default void setWarningMin(RecyclerHolder holder, int id, float max) {

        if (null == holder) return;
        holder.setMinWarning(id, max);
    }

    default void setInputDotLength(RecyclerHolder holder, int id, float max) {

        if (null == holder) return;
        holder.setInputMin(id, max);
    }

    default void setText(RecyclerHolder holder, int id, int strId) {

        if (null == holder) return;
        final Context context = BaseApp.getContext();
        final String strs = context.getResources().getString(strId);
        holder.setText(id, strs);
    }

    default void setTextColor(RecyclerHolder holder, int id, int color) {

        if (null == holder) return;
        holder.setTextColor(id, color);
    }

    default void setVisibility(RecyclerHolder holder, int id, int visable) {

        if (null == holder) return;
        holder.setVisible(id, visable);
    }

    default void setVisibility(View view, int visable) {

        if (null == view) return;
        view.setVisibility(visable);
    }

    default void setRecycler(RecyclerView recycler, RecyclerView.Adapter adapter, RecyclerView.LayoutManager manager) {
        setRecycler(recycler, adapter, manager, null, null);
    }

    default void setRecycler(RecyclerView recycler, RecyclerView.Adapter adapter, RecyclerView.LayoutManager manager, RecyclerView.ItemDecoration decoration) {
        setRecycler(recycler, adapter, manager, decoration, null);
    }

    default void setRecycler(RecyclerView recycler, RecyclerView.Adapter adapter, RecyclerView.LayoutManager manager, SnapHelper snapHelper) {
        setRecycler(recycler, adapter, manager, null, snapHelper);
    }

    default void setRecycler(RecyclerView recycler, RecyclerView.Adapter adapter, RecyclerView.LayoutManager manager, RecyclerView.ItemDecoration decoration, SnapHelper snapHelper) {
        if (null == recycler || null == adapter || null == manager) return;
        if (null != decoration) {
            recycler.addItemDecoration(decoration);
        }
        recycler.setLayoutManager(manager);
        if (null != snapHelper) {
            snapHelper.attachToRecyclerView(recycler);
        }
        recycler.setAdapter(adapter);
    }

    default void setItemDecoration(RecyclerView recycler, RecyclerView.ItemDecoration decoration) {
        if (null == recycler || null == decoration) return;
        recycler.addItemDecoration(decoration);
    }

    default void refreshList(RecyclerView recycler) {
        refreshList(recycler, false);
    }

    default void refreshList(RecyclerView recycler, int position) {

        if (null == recycler || position < 0)
            return;

        final int itemCount = recycler.getAdapter().getItemCount();
        if (position >= itemCount)
            return;

        recycler.getAdapter().notifyItemRangeChanged(position, 1);
    }

    default void setListNull(RecyclerView recycler, int layoutId) {

        if (null == recycler) return;

        RecyclerView.Adapter adapter = recycler.getAdapter();
        if (!(adapter instanceof BaseCommonAdapter)) return;

        BaseCommonAdapter adapter1 = (BaseCommonAdapter) adapter;
        adapter1.setNullView(BaseApp.getContext(), layoutId);
    }

    default void refreshList(RecyclerView recycler, boolean clearTag) {
        if (null == recycler) return;

        if (clearTag) {
            for (int i = 0; i < recycler.getChildCount(); i++) {
                final View child = recycler.getChildAt(i);
                if (null == child) continue;
                final Object tag = child.getTag();
                if (null == tag) continue;

                // LogUtil.e("refreshList", "第" + i + "个孩子置空TAG");
                child.setTag(null);
            }
        }

        recycler.getAdapter().notifyDataSetChanged();
    }

//    default void refreshList(RecyclerView recycler, int position) {
//        if (null == recycler) return;
//        recycler.getAdapter().notifyItemRangeChanged(position, 1);
//    }
//
//    default void refreshList(RecyclerView recycler, int position, int count) {
//        if (null == recycler) return;
//        recycler.getAdapter().notifyItemRangeChanged(position, count);
//    }

    default void setChart(ChartView chart, List<? extends Entry> list) {
        if (null == chart || null == list) return;
        chart.notifyDataSetChanged(list);
        if (list.size() != 0) return;
        setChartNull(chart, R.string.chart_null);
    }

    default void setChartNull(ChartView chart, int textId) {
        if (null == chart) return;
        final String string = BaseApp.getResource().getString(textId);
        if (TextUtils.isEmpty(string)) return;
        chart.clearList();
        chart.setHintText(string);
    }

    default void scrollRecycler(RecyclerView recycler, int position) {
        if (null == recycler || position < 0) return;
        recycler.smoothScrollToPosition(position);
    }

    default void setInputEnable(EditText text, boolean enable) {
        if (null == text) return;
        text.setCursorVisible(enable);
    }

    default void setInputEnable(RecyclerHolder holder, int viewId, boolean enable) {
        if (null == holder) return;
        holder.setInputEnable(viewId, enable);
    }

    default <T extends View> T getHead(RecyclerView recycler, int viewId) {

        if (null == recycler) return null;

        RecyclerView.Adapter recyclerAdapter = recycler.getAdapter();
        if (null == recyclerAdapter || !(recyclerAdapter instanceof BaseCommonAdapter))
            return null;

        BaseCommonAdapter adapter = (BaseCommonAdapter) recyclerAdapter;
        LinearLayout head = adapter.getHead();
        if (null == head) return null;

        return (T) head.findViewById(viewId);
    }
}
